package com.dianping.cat.alarm.sender.transform;

import com.dianping.cat.alarm.sender.IEntity;
import com.dianping.cat.alarm.sender.IVisitor;
import com.dianping.cat.alarm.sender.entity.Par;
import com.dianping.cat.alarm.sender.entity.Sender;
import com.dianping.cat.alarm.sender.entity.SenderConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;













public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

  public DefaultXmlBuilder() { this(false); }



  public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  40 */     this.m_compact = compact;
/*  41 */     this.m_sb = sb;
/*  42 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  46 */     entity.accept(this.m_visitor);
/*  47 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  51 */     this.m_level--;

/*  53 */     indent();
/*  54 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  58 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  62 */     if (value == null) {
/*  63 */       return null;
    }

/*  66 */     String str = toString(value);
/*  67 */     int len = str.length();
/*  68 */     StringBuilder sb = new StringBuilder(len + 16);

/*  70 */     for (int i = 0; i < len; i++) {
/*  71 */       char ch = str.charAt(i);

/*  73 */       switch (ch) {
        case '<':
/*  75 */           sb.append("&lt;");
          break;
        case '>':
          sb.append("&gt;");
          break;
        case '&':
/*  81 */           sb.append("&amp;");
          break;
        case '"':
/*  84 */           if (!text) {
/*  85 */             sb.append("&quot;");
            break;
          }
        default:
/*  89 */           sb.append(ch);
          break;
      }

    }
/*  94 */     return sb.toString();
  }

  protected void indent() {
/*  98 */     if (!this.m_compact) {
/*  99 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 100 */         this.m_sb.append("   ");
      }
    }
  }


/* 106 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 110 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 114 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 118 */     indent();

/* 120 */     this.m_sb.append('<').append(name);

/* 122 */     int len = nameValues.length;

/* 124 */     for (int i = 0; i + 1 < len; i += 2) {
/* 125 */       Object attrName = nameValues[i];
/* 126 */       Object attrValue = nameValues[i + 1];

/* 128 */       if (attrValue != null) {
/* 129 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 133 */     if (dynamicAttributes != null) {
/* 134 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 135 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 139 */     if (text != null && closed) {
/* 140 */       this.m_sb.append('>');
/* 141 */       this.m_sb.append(escape(text, true));
/* 142 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 144 */       if (closed) {
/* 145 */         this.m_sb.append('/');
      } else {
/* 147 */         this.m_level++;
      }

/* 150 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 156 */     if (value instanceof String)
/* 157 */       return (String)value; 
/* 158 */     if (value instanceof Collection) {
/* 159 */       Collection<Object> list = (Collection)value;
/* 160 */       StringBuilder sb = new StringBuilder(32);
/* 161 */       boolean first = true;

/* 163 */       for (Object item : list) {
/* 164 */         if (first) {
/* 165 */           first = false;
        } else {
/* 167 */           sb.append(',');
        }

/* 170 */         if (item != null) {
/* 171 */           sb.append(item);
        }
      }

/* 175 */       return sb.toString();
/* 176 */     }  if (value.getClass().isArray()) {
/* 177 */       int len = Array.getLength(value);
/* 178 */       StringBuilder sb = new StringBuilder(32);
/* 179 */       boolean first = true;

/* 181 */       for (int i = 0; i < len; i++) {
/* 182 */         Object item = Array.get(value, i);

/* 184 */         if (first) {
/* 185 */           first = false;
        } else {
/* 187 */           sb.append(',');
        }

/* 190 */         if (item != null) {
/* 191 */           sb.append(item);
        }
      }

/* 195 */       return sb.toString();
    }

/* 198 */     return String.valueOf(value);
  }



/* 203 */   public void visitPar(Par par) { startTag("par", true, null, new Object[] { "id", par.getId() }); }



  public void visitSender(Sender sender) {
/* 208 */     startTag("sender", null, new Object[] { "id", sender.getId(), "url", sender.getUrl(), "type", sender.getType(), "successCode", sender.getSuccessCode(), "batchSend", Boolean.valueOf(sender.isBatchSend()) });

/* 210 */     if (!sender.getPars().isEmpty()) {
/* 211 */       for (Par par : sender.getPars()) {
/* 212 */         par.accept(this.m_visitor);
      }
    }

/* 216 */     endTag("sender");
  }


  public void visitSenderConfig(SenderConfig senderConfig) {
/* 221 */     startTag("sender-config", null, new Object[0]);

/* 223 */     if (!senderConfig.getSenders().isEmpty()) {
/* 224 */       for (Sender sender : senderConfig.getSenders().values()) {
/* 225 */         sender.accept(this.m_visitor);
      }
    }

/* 229 */     endTag("sender-config");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/sender/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */